package com.antdesignbackend.backend.news.notice.service.impl;


import com.antdesignbackend.backend.entity.Users;
import com.antdesignbackend.backend.news.notice.entity.entityVo.NoticeVo;
import com.antdesignbackend.backend.news.noticeusers.entity.NoticeUsers;
import com.antdesignbackend.backend.news.noticeusers.service.NoticeUsersService;
import com.antdesignbackend.backend.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.antdesignbackend.backend.news.notice.mapper.NoticeMapper;
import com.antdesignbackend.backend.news.notice.entity.Notice;
import com.antdesignbackend.backend.news.notice.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    NoticeMapper noticeMapper;

    @Autowired
    NoticeService noticeService;

    @Autowired
    UserService userService;

    @Autowired
    NoticeUsersService noticeUsersService;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void createNotice(Notice notice) throws Exception {
        //获得发布人的权限区域
        Users u = userService.getById(notice.getCreateUid());
        if (u != null){
            notice.setAuthCity(u.getAuthCity());
            notice.setAuthDistrict(u.getAuthDistrict());
            notice.setAuthTown(u.getAuthTown());
        }
        if(noticeService.save(notice)){
            //根据角色查询到对应的用户并添加到用户消息列表
            /**
             * 养殖企业单独判断
             */
            String[] s = notice.getRoles().split(",");

            QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
            //判断用户权限
            if (u.getAuthType() == 1){ //地市权限
                queryWrapper.eq("AuthCity",u.getAuthCity());
            }else if (u.getAuthType() == 2){ //区县权限
                queryWrapper.eq("AuthDistrict",u.getAuthDistrict());
            }else if (u.getAuthType() == 3){
                queryWrapper.eq("AuthTown",u.getAuthTown());
            }
            queryWrapper.in("userType", s);
            List<Users> list = userService.list(queryWrapper);
            if (list.size() > 0){
                List<NoticeUsers> noticeUsersList = new ArrayList<>();
                for (Users users:list){
                    NoticeUsers noticeUsers = new NoticeUsers();
                    noticeUsers.setNid(notice.getId());
                    noticeUsers.setUid(users.getUserID());
                    noticeUsers.setStatus(0);
                    noticeUsers.setCreateTime(new Date());
                    noticeUsersList.add(noticeUsers);
                }
                noticeUsersService.saveBatch(noticeUsersList);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void updateNotice(Notice notice) throws Exception {
        //获得发布人的权限区域
        Users u = userService.getById(notice.getCreateUid());
        if(noticeService.updateById(notice)){
            //删除之前的
            QueryWrapper<NoticeUsers> wrapper = new QueryWrapper<>();
            wrapper.eq("nid",notice.getId());
            noticeUsersService.remove(wrapper);
            //根据角色查询到对应的用户并添加到用户消息列表
            /**
             * 养殖企业单独判断
             */
            String[] s = notice.getRoles().split(",");

            QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
            //判断用户权限
            if (u.getAuthType() == 1){ //地市权限
                queryWrapper.eq("AuthCity",u.getAuthCity());
            }else if (u.getAuthType() == 2){ //区县权限
                queryWrapper.eq("AuthDistrict",u.getAuthDistrict());
            }else if (u.getAuthType() == 3){
                queryWrapper.eq("AuthTown",u.getAuthTown());
            }
            queryWrapper.in("userType", s);
            List<Users> list = userService.list(queryWrapper);
            if (list.size() > 0){
                List<NoticeUsers> noticeUsersList = new ArrayList<>();
                for (Users users:list){
                    NoticeUsers noticeUsers = new NoticeUsers();
                    noticeUsers.setNid(notice.getId());
                    noticeUsers.setUid(users.getUserID());
                    noticeUsers.setStatus(0);
                    noticeUsers.setCreateTime(new Date());
                    noticeUsersList.add(noticeUsers);
                }
                noticeUsersService.saveBatch(noticeUsersList);
            }
        }
    }

    @Override
    public List<NoticeVo> search(QueryWrapper<NoticeVo> wrapper, Page<NoticeVo> page) {
        return noticeMapper.search(wrapper,page);
    }

    @Override
    public Integer count(QueryWrapper<NoticeVo> wrapper) {
        return noticeMapper.count(wrapper);
    }
}
